#include <stdlib.h>
#include <stdio.h>
#include <map>
#include <Ice/Ice.h>
#include "event.h"

Event::Event(const Ice::CommunicatorPtr& c)
{
	comm = c;
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(c);
	os->writeInt(DEFAULT_EVENT_TYPE);
	os->finished(ev);
	
	setMemberValue("type", ev);
}

Event::Event(const ::Collaboration::Event& ev, const Ice::CommunicatorPtr& c)
{
	comm = c;
	event = ev;
}

Event::~Event()
{
	event.clear();
}

bool Event::existsMember(const ::std::string& name)
{
	std::map<std::string,Collaboration::EventData>::iterator iter = event.find(name);
	if (iter != event.end()) return true;
	else return false;
}

Collaboration::EventData Event::getMemberValue(const ::std::string& name, bool& exists)
{
	Collaboration::EventData ev;
	
	std::map<std::string,Collaboration::EventData>::iterator iter = event.find(name);
	if (iter != event.end())
	{
		ev = iter->second;
		exists = true;
	}
	else
	{
		exists = false;
	}
	
	return ev;
}

Collaboration::EventData Event::getMemberValue(const ::std::string& name)
{
	Collaboration::EventData ev;
	
	std::map<std::string,Collaboration::EventData>::iterator iter = event.find(name);
	if (iter != event.end())
	{
		ev = iter->second;
	}
	
	return ev;
}

void Event::setMemberValue(const ::std::string& name, const Collaboration::EventData& value)
{
	std::map<std::string,Collaboration::EventData>::iterator iter = event.find(name);
	if (iter != event.end()) //si existe hay que borrarlo primero
	{
		event.erase(iter);
	}
		
	event[name] = value;
}

unsigned int Event::countMembers()
{
	return event.size();
}

int Event::getType()
{
	Collaboration::EventData ev = getMemberValue("type");
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readInt();
}

Collaboration::Event Event::internalEvent() const
{
	return event;
}

bool Event::interpretBool(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readBool();
}

::Ice::Byte Event::interpretByte(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readByte();
}

short Event::interpretShort(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readShort();
}

int Event::interpretInt(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readInt();
}

long Event::interpretLong(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readLong();
}

float Event::interpretFloat(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readFloat();
}

double Event::interpretDouble(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readDouble();
}

::std::string Event::interpretString(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readString();
}

::Ice::ObjectPrx Event::interpretObjectPrx(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readProxy();
}

std::vector<bool> Event::interpretBoolSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readBoolSeq();
}

std::vector< ::Ice::Byte> Event::interpretByteSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readByteSeq();
}

std::vector<short> Event::interpretShortSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readShortSeq();
}

std::vector<int> Event::interpretIntSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readIntSeq();
}

std::vector< ::Ice::Long> Event::interpretLongSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readLongSeq();
}

std::vector<float> Event::interpretFloatSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readFloatSeq();
}

std::vector<double> Event::interpretDoubleSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readDoubleSeq();
}

std::vector< ::std::string> Event::interpretStringSeq(const Collaboration::EventData& ev)
{
	Ice::InputStreamPtr os = Ice::createInputStream(comm, ev);
	return os->readStringSeq();
}

Collaboration::EventData Event::fromBool(bool b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeBool(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromByte(::Ice::Byte b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeByte(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromShort(short b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeShort(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromInt(int b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeInt(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromLong(long b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeLong(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromFloat(float b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeFloat(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromDouble(double b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeDouble(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromString(const ::std::string& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeString(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromObjectPrx(const ::Ice::ObjectPrx& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeProxy(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromBoolSeq(const std::vector<bool>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeBoolSeq(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromByteSeq(const std::vector< ::Ice::Byte>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeByteSeq(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromShortSeq(const std::vector<short>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeShortSeq(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromIntSeq(const std::vector<int>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeIntSeq(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromLongSeq(const std::vector< ::Ice::Long>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeLongSeq(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromFloatSeq(const std::vector<float>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeFloatSeq(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromDoubleSeq(const std::vector<double>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeDoubleSeq(b);
	os->finished(ev);
	return ev;
}

Collaboration::EventData Event::fromStringSeq(const std::vector< ::std::string>& b)
{
	Collaboration::EventData ev;
	Ice::OutputStreamPtr os = Ice::createOutputStream(comm);
	os->writeStringSeq(b);
	os->finished(ev);
	return ev;
}
